package com.biShe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.biShe.entity.Category;
import com.biShe.entity.User;
import com.biShe.mapper.CategoryMapper;
import com.biShe.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小刘
 * @since 2023-05-06
 */
@Configuration
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查找所有分类
     *
     * @return 返回所有分类信息列表
     */
    @ApiOperation(value="获取所有分类信息", notes="返回所有分类列表")
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") String page, @PathVariable("size") String size) {
        Page pageHelper = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        List<Category> categoryList = categoryMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(categoryList);
        return Result.succ(pageInfo);
    }

    /**
     * 查找所有分类
     *
     * @return 返回所有分类信息列表
     */
    @ApiOperation(value="获取所有分类信息", notes="返回所有分类信息列表")
    @GetMapping("/list")
    public Result list() {
        List<Category> categoryList = categoryMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(categoryList);
        return Result.succ(pageInfo);
    }


    /**
     * 根据ID查找分类
     *
     * @param id 分类ID
     * @return 返回指定ID的分类信息
     */
    @ApiOperation(value="根据ID获取分类信息", notes="返回指定ID的分类信息")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Int", paramType = "path")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            return Result.fail("该分类不存在");
        }
        return Result.succ(category);
    }

    /**
     * 添加新分类
     *
     * @param category 新分类信息
     * @return 返回添加结果
     */
    @ApiOperation(value="添加新分类", notes="添加新分类信息")
    @ApiImplicitParam(name = "category", value = "分类实体", required = true, dataType = "Category")
    @PostMapping("/")
    public Result add(@RequestBody Category category) {
        //获取时间戳
        long l = System.currentTimeMillis();
        System.out.println("前端获取的category");
        System.out.println(category);
        //格式化
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = format.format(l);
        System.out.println(s);
        category.setCreatedTime(s);
        System.out.println(category);

        int rows = categoryMapper.insert(category);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",category.getName());
//        queryWrapper.eq("is_use",category.getIsUse());
//        queryWrapper.eq("is_activity",category.getIsActivity());
        queryWrapper.eq("created_time",s);
//        queryWrapper.eq("text",category.getText());

//        int rows = categoryMapper.insert(category);
        Category category1 = categoryMapper.selectOne(queryWrapper);
        if (rows == 0) {
            return Result.fail("添加新分类失败");
        }
        return Result.succ(category1);
    }

    /**
     * 更新分类信息
     *
     * @param category 分类信息
     * @return 返回更新结果
     */
    @ApiOperation(value="修改分类信息", notes="修改指定分类的信息")
    @ApiImplicitParam(name = "category", value = "分类实体", required = true, dataType = "Category")
    @PutMapping("/")
    public Result update(@RequestBody Category category) {
        int rows = categoryMapper.updateById(category);
        if (rows == 0) {
            return Result.fail("更新分类信息失败");
        }
        return Result.succ("更新分类信息成功");
    }

    /**
     * 删除分类信息
     *
     * @param id 分类ID
     * @return 返回删除结果
     */
    @ApiOperation(value="删除分类信息", notes="删除指定ID的分类信息")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Int", paramType = "path")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int rows = categoryMapper.deleteById(id);
        if (rows == 0) {
            return Result.fail("删除分类信息失败");
        }
        return Result.succ("删除分类信息成功");
    }


    /**
     * 根据分类名模糊查询分类信息
     *
     * @param name 分类名
     * @return 返回符合条件的分类信息列表
     */
    @ApiOperation(value="根据分类名模糊查询分类信息", notes="返回符合条件的分类信息列表")
    @ApiImplicitParam(name = "name", value = "分类名", required = true, dataType = "String", paramType = "path")
    @GetMapping("/search/{name}")
    public Result search(@PathVariable("name") String name) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        List<Category> categoryList = categoryMapper.selectList(wrapper);
        if (categoryList.isEmpty()) {
            return Result.fail("暂无匹配结果");
        }
        return Result.succ(categoryList);
    }

}

